#!/bin/bash
# default mysql root passwd: root
IP=`/sbin/ifconfig | grep 'inet addr' | grep -v 127.0.0.1 | awk '{print $2}' | awk -F ':' '{print $2}'`
tar zxvf cacti-1.1.38.tar.gz -C /opt/
ln -s /opt/cacti-1.1.38/ /opt/cacti
#set mysql
mysql -uroot -proot -e "create database cacti default charset utf8;"
mysql -uroot -proot cacti < /opt/cacti/cacti.sql
mysql -uroot -proot -e "
GRANT ALL PRIVILEGES ON cacti.* TO 'cacti'@'192.168.%.%' IDENTIFIED BY 'cacti';
GRANT ALL PRIVILEGES ON mysql.time_zone TO 'cacti'@'192.168.%.%' IDENTIFIED BY 'cacti';
GRANT ALL PRIVILEGES ON mysql.time_zone_name TO 'cacti'@'192.168.%.%' IDENTIFIED BY 'cacti';
flush privileges;"
/usr/local/mysql/bin/mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -proot mysql

#install yum php net-snmp rrdtool 

rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm
yum install httpd php56w php56w-gd php56w-mysql php56w-bcmath php56w-mbstring php56w-xml php56w-ldap php56w-process  php56w-snmp -y
yum install -y net-snmp net-snmp-libs net-snmp-utils rrdtool
yum install -y gcc gcc-c++ make autoconf automake libtool

cp cacti.conf /etc/httpd/conf.d/
sed -i '/$database_username/s/cactiuser/cacti/' /opt/cacti/include/config.php 
sed -i '/$database_password/s/cactiuser/cacti/' /opt/cacti/include/config.php 
sed -i "/\$database_hostname/s/localhost/${IP}/" /opt/cacti/include/config.php
touch /opt/cacti/log/cacti.log
chown -R apache.apache /opt/cacti/
# timezone for php.ini
if [ !$(grep -c ^date.timezone /etc/php.ini) ];then
sed -i '/; date.timezone/idate.timezone = Asia/Shanghai' /etc/php.ini
fi
service httpd restart
